using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Speedball2 {
  static class Program {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main () {
      //SortedList<int, int> samples = new SortedList<int, int> ();
      //for (int i = 0; i < 43; i++) {
      //  int length = (int) Game.AmigaMatch.ReadWord (0x35F7C + i * 14 + 4);
      //  int start = 0x1BC3E + (int) Game.AmigaMatch.ReadLong (0x35F7C + i * 14 + 6);
      //  int end = start + length;
      //  if (samples.ContainsKey (start))
      //  Console.WriteLine ("{0,8:X} {1,8:X} {2}", start, end, length);
      //  else
      //    samples.Add (start, end);
      //}
      //Console.WriteLine ();
      //foreach (KeyValuePair<int,int> kvp in samples) {
      //  Console.WriteLine ("{0,8:X} {1,8:X}", kvp.Key, kvp.Value);
      //}
      //Environment.Exit (0);

      //Pc1 pc1 = new Pc1 (ExtensionByte.CreateFromFile ("doors.pc1"), Sprite.MasksEnum.Opaque);
      //return;

      //byte [] bytesDisk = Resource.Speedball2_Amiga;
      //byte [] bytesRam = Resource.Match_Amiga;
      //byte [] bytesIntro = Resource.Intro_Amiga;
      //bytesIntro.Extract (0x5AE00, 6880).WriteToFile ("logofont.spr");


      ////ExtractIlbm (bytes, 0x68E00).WriteToFile ("ilbm1.ilbm");
      ////ExtractIlbm (bytes, 0x90C00).WriteToFile ("ilbm2.ilbm");
      ////ExtractIlbm (bytes, 0x97800).WriteToFile ("ilbm3.ilbm");
      ////ExtractIlbm (bytes, 0x9D000).WriteToFile ("ilbm4.ilbm");
      ////ExtractIlbm (bytes, 0xA1000).WriteToFile ("ilbm5.ilbm");
      ////ExtractIlbm (bytes, 0xA5000).WriteToFile ("ilbm6.ilbm");
      ////ExtractIlbm (bytes, 0xA9000).WriteToFile ("ilbm7.ilbm");
      ////ExtractIlbm (bytes, 0x49E5E).WriteToFile ("stadium.ilbm");
      ////ExtractIlbm (bytes, 0x52D0E).WriteToFile ("doors.ilbm");
      //bytesDisk.Extract (0xC2C00, 57280).WriteToFile ("pitch.dat");

      //Palette paletteMatch = new Palette (bytesRam, 0xF2, 32, Palette.PaletteLookup.Amiga);

      //Bitmap [] bitmaps = Sprite.CreateBitmaps (bytesDisk, 0xC2C00, 358, 16, 16, 5, Sprite.ModesEnum.Planar, -1, paletteMatch, 0, Sprite.MasksEnum.Opaque);
      //if (!Directory.Exists ("Amiga/pitch_dat")) {
      //  Directory.CreateDirectory ("Amiga/pitch_dat");
      //  int i = 0;
      //  foreach (Bitmap bitmap in bitmaps)
      //    bitmap.Save (string.Format ("Amiga/pitch_dat/{0}.png", i++), ImageFormat.Png);
      //}

      //bitmaps = Sprite.CreateBitmaps (bytesDisk, 0xAD000, 89, 16, 16, 4, Sprite.ModesEnum.Planar, -1, paletteMatch, 0, Sprite.MasksEnum.Opaque);
      //if (!Directory.Exists ("Amiga/balls_spr")) {
      //  Directory.CreateDirectory ("Amiga/balls_spr");
      //  int i = 0;
      //  foreach (Bitmap bitmap in bitmaps)
      //    bitmap.Save (string.Format ("Amiga/balls_spr/{0}.png", i++), ImageFormat.Png);
      //}

      //return;

      VersionEnum version = VersionEnum.Amiga;
      string [] args = Environment.GetCommandLineArgs ();
      if (args.Length > 1)
        if (args [1] == "-Atari")
          version = VersionEnum.Atari;
        else if (args [1] == "-Amiga")
          version = VersionEnum.Amiga;

      Application.EnableVisualStyles ();
      Application.SetCompatibleTextRenderingDefault (false);
      Application.Run (new Main (version));
    }

    private static byte [] ExtractIlbm (byte [] bytes, int address) {
      long form = bytes.ReadLong (address);
      if (form != 0x464F524D)
        throw new Exception ();
      int length = (int) bytes.ReadLong (address + 4);
      return (bytes.Extract (address, length + 8));
    }
  }
}
